package com.chuangshu.chuangshuteam.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chuangshu.chuangshuteam.config.BaseContext;
import com.chuangshu.chuangshuteam.dto.Result;
import com.chuangshu.chuangshuteam.entity.Likes;
import com.chuangshu.chuangshuteam.entity.Posts;
import com.chuangshu.chuangshuteam.service.LikesService;
import com.chuangshu.chuangshuteam.service.PostsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author k
 * @since 2025-09-21
 */
@RestController
@RequestMapping("/likes")
public class LikesController {

    @Autowired
    private LikesService likesService;
    
    @Autowired
    private PostsService postsService;

    /**
     * 点赞或取消点赞帖子
     * @param postId 帖子ID
     * @return 点赞结果
     */
    @PostMapping("/posts/{postId}")
    public Result likePost(@PathVariable Long postId) {
        // 检查帖子是否存在
        Posts post = postsService.getById(postId);
        if (post == null) {
            return Result.fail("帖子不存在");
        }

        Long userId = BaseContext.getCurrentId();
        
        // 检查是否已经点赞
        LambdaQueryWrapper<Likes> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Likes::getUserId, userId)
                .eq(Likes::getLikableId, postId)
                .eq(Likes::getLikableType, "posts");
        
        Likes existingLike = likesService.getOne(queryWrapper);
        
        if (existingLike != null) {
            // 已经点赞，执行取消点赞操作
            likesService.remove(queryWrapper);
            return Result.ok("取消点赞成功");
        } else {
            // 未点赞，执行点赞操作
            Likes like = new Likes();
            like.setUserId(userId);
            like.setLikableId(postId);
            like.setLikableType("posts");
            like.setCreatedAt(LocalDateTime.now());
            likesService.save(like);
            return Result.ok("点赞成功");
        }
    }
    
    /**
     * 获取帖子点赞状态
     * @param postId 帖子ID
     * @return 点赞状态
     */
    @GetMapping("/posts/{postId}/status")
    public Result getLikeStatus(@PathVariable Long postId) {
        // 检查帖子是否存在
        Posts post = postsService.getById(postId);
        if (post == null) {
            return Result.fail("帖子不存在");
        }
        
        Long userId = BaseContext.getCurrentId();
        
        // 检查是否已经点赞
        LambdaQueryWrapper<Likes> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Likes::getUserId, userId)
                .eq(Likes::getLikableId, postId)
                .eq(Likes::getLikableType, "posts");
        
        boolean isLiked = likesService.count(queryWrapper) > 0;
        
        return Result.ok(isLiked);
    }
    
    /**
     * 获取帖子点赞数
     * @param postId 帖子ID
     * @return 点赞数
     */
    @GetMapping("/posts/{postId}/count")
    public Result getLikeCount(@PathVariable Long postId) {
        // 检查帖子是否存在
        Posts post = postsService.getById(postId);
        if (post == null) {
            return Result.fail("帖子不存在");
        }
        
        // 统计点赞数
        LambdaQueryWrapper<Likes> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Likes::getLikableId, postId)
                .eq(Likes::getLikableType, "posts");
        
        long count = likesService.count(queryWrapper);
        
        return Result.ok(count);
    }
}